<?php

$GLOBALS['tbl_prefix'] = $CONFIG['tbl_prefix']; 

function count_posts() {
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$now = date("Y-m-d H:i:s");
	$sql = "SELECT count(*) as mycount FROM ".$tbl_prefix."content WHERE public='1' AND pubdate < '".$now."'"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	while($row = mysql_fetch_assoc($result)) { 
		$x = $row['mycount'];
	} 	
	return $x;
}

function get_related_posts($topic,$id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT ".$tbl_prefix."content.title,".$tbl_prefix."content.id,".$tbl_prefix."content.slug 
	FROM ".$tbl_prefix."content 
	WHERE ".$tbl_prefix."content.topic='".$topic."' AND ".$tbl_prefix."content.id <> '".$id."' AND "
	.$tbl_prefix."content.public='1' ORDER BY id DESC LIMIT 0,5";
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$x[] = $row;
	} 	
	return $x;
}

function get_related_by_tag($tags,$id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT ".$tbl_prefix."content.id as id, ".$tbl_prefix."content.title as title, ".$tbl_prefix."tags.tag as tag,
	".$tbl_prefix."tags.tagname as tagname,".$tbl_prefix."content.slug as slug
	FROM ".$tbl_prefix."content  
	LEFT JOIN ".$tbl_prefix."tags ON ".$tbl_prefix."content.id=tags.id
	WHERE ".$tbl_prefix."content.public='1' AND ".$tbl_prefix."content.id <> '".$id."' AND (";
	if(!is_array($tags)) { return false; } 
	foreach($tags as $k=>$v) { 
		$sql2 .= $tbl_prefix."tags.tagname LIKE '%".$v."%' OR ";
	}
	$sql .= substr(trim($sql2),0,-2).")"; 
	$sql .= " GROUP BY ".$tbl_prefix."content.id ORDER BY ".$tbl_prefix."content.pubdate DESC LIMIT 0,7"; 
	//die($sql); 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$x[] = $row;
	} 	
	return $x;
}

function get_tags($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT tagid,tag FROM ".$tbl_prefix."tags WHERE id='".$id."'";
	$result = mysql_query($sql); $GLOBALS['q']++;
	while($row = mysql_fetch_assoc($result)) { 
		$x[$row['tagid']] = $row['tag'];
	} 	
	return $x;	 
}


function get_posts($offset,$display,$topic='0') { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT ".$tbl_prefix."content.*,".$tbl_prefix."content.id as thisid, ".$tbl_prefix."topics.name as tName,"
	.$tbl_prefix."topics.img as tImg,".$tbl_prefix."topics.topicid,
	".$tbl_prefix."users.realname as realname,COUNT(".$tbl_prefix."comments.commentid) as mycount
	FROM ".$tbl_prefix."content  
	LEFT JOIN ".$tbl_prefix."users on ".$tbl_prefix."content.uid=".$tbl_prefix."users.uid
	LEFT JOIN ".$tbl_prefix."topics ON ".$tbl_prefix."content.topic=".$tbl_prefix."topics.topicid
	LEFT JOIN ".$tbl_prefix."comments ON ".$tbl_prefix."content.id=".$tbl_prefix."comments.id	
	WHERE ".$tbl_prefix."content.public='1'";
	if($topic!='0') { 
		$topic = intval($topic); $sql .= " AND ".$tbl_prefix."content.topic='".$topic."'"; 
	}
	$sql .= " GROUP BY ".$tbl_prefix."content.id ORDER BY ".$tbl_prefix."content.pubdate DESC LIMIT ".$offset.",".$display; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) {
		$row['tags'] = array(); 
		$row['tags'] = get_tags($row['thisid']); 		
		$x[] = $row;
	} 	
	return $x;
}

function get_posts_by_tag($tag,$like=0) {
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	if($like==1) { $l="%"; } else { $l=""; } 
	$sql = "SELECT cn.*,cn.id as thisid, tp.name as tName,tp.img as tImg,tp.topicid,
	u.realname as realname,COUNT(cm.commentid) as mycount, t.tag as tags
	FROM ".$tbl_prefix."content cn 
	LEFT JOIN ".$tbl_prefix."users u on cn.uid=u.uid
	LEFT JOIN ".$tbl_prefix."topics tp ON cn.topic=tp.topicid
	LEFT JOIN ".$tbl_prefix."comments cm ON cn.id=cm.id	
	LEFT JOIN ".$tbl_prefix."tags t ON cn.id=t.id
	WHERE cn.public='1' AND t.tagname LIKE '".$l.mysql_real_escape_string($tag).$l."'
	GROUP BY cn.id ORDER BY cn.pubdate DESC LIMIT 0,20"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$row['tags'] = array(); 
		$row['tags'] = get_tags($row['thisid']); 
		$x[] = $row;
	} 	
	return $x;
}

function get_all_posts($offset,$display) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT * FROM ".$tbl_prefix."content ORDER BY pubdate DESC LIMIT $offset,$display"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$row['tags'] = array(); 
		$row['tags'] = get_tags($row['id']); 		
		$x[] = $row;
	} 	
	return $x;
}

function get_post($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT ".$tbl_prefix."content.*,".$tbl_prefix."topics.*,".$tbl_prefix."users.realname
	FROM ".$tbl_prefix."content 
	LEFT JOIN ".$tbl_prefix."users on ".$tbl_prefix."content.uid=".$tbl_prefix."users.uid
	LEFT JOIN ".$tbl_prefix."tags ON ".$tbl_prefix."content.id=".$tbl_prefix."tags.id	
	LEFT JOIN ".$tbl_prefix."topics on ".$tbl_prefix."content.topic=topics.topicID WHERE ".$tbl_prefix."content.id='".intval($id)."'"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	$x = mysql_fetch_assoc($result);
	$x['tags'] = get_tags($x['id']); 
	return $x;
}

function get_post_by_slug($slug) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT ".$tbl_prefix."content.*,".$tbl_prefix."topics.*,".$tbl_prefix."users.realname
	FROM ".$tbl_prefix."content 
	LEFT JOIN ".$tbl_prefix."users on ".$tbl_prefix."content.uid=".$tbl_prefix."users.uid
	LEFT JOIN ".$tbl_prefix."tags ON ".$tbl_prefix."content.id=".$tbl_prefix."tags.id	
	LEFT JOIN ".$tbl_prefix."topics on ".$tbl_prefix."content.topic=".$tbl_prefix."topics.topicID 
	WHERE ".$tbl_prefix."content.slug='".mysql_real_escape_string($slug)."'"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	$x = mysql_fetch_assoc($result);
	$x['tags'] = get_tags($x['id']); 
	return $x;
}

function is_blogged($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT count(*) as mycount FROM ".$tbl_prefix."content WHERE id='".intval($id)."'"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	$x = mysql_fetch_assoc($result);
	if($x['mycount'] < 1) { 
		return FALSE; 
	} else { 
		return TRUE;
	}
}

function add_view($id,$views) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "UPDATE ".$tbl_prefix."content SET views='".($views+1)."' WHERE id='".$id."'";
	$result = mysql_query($sql); $GLOBALS['q']++;
	return TRUE; 
}

function new_comment($name,$id,$contact,$title,$comment) {
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	global $ip,$debug_mode;
	$now = time();
	$sql  = "INSERT into ".$tbl_prefix."comments (commentid,id,name,contact,title,text,ip,score) values ('".$now."','".$id."','"
	. mysql_real_escape_string($name)."','".mysql_real_escape_string($contact)."','"
	. mysql_real_escape_string($title)."','".mysql_real_escape_string($comment)."','".$ip."','1')";
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	return $now;
}

function count_comments($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT count(*) as mycount FROM ".$tbl_prefix."comments WHERE id='".intval($id)."' AND score>0"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$x = $row['mycount'];
	} 	
	return $x;
}

function get_comments($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT * FROM ".$tbl_prefix."comments WHERE id='".intval($id)."' AND score>0 ORDER BY commentid ASC"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$x[] = $row;
	} 	
	return $x;
}

function get_topic($topicid) {
	$tbl_prefix = $GLOBALS['tbl_prefix'];  
	$sql = "SELECT * FROM ".$tbl_prefix."topics WHERE topicid ='".intval($topicid)."' ORDER BY name";
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	while($row = mysql_fetch_assoc($result)) { 
		$x = $row;
	} 	
	return $x;	
}

function get_topics() { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT topicid, name, img FROM ".$tbl_prefix."topics ORDER BY name";
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	while($row = mysql_fetch_assoc($result)) { 
		$x[$row['topicid']] = $row;
	} 	
	return $x;	
}

function get_rss($length=10,$topic='0') { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT c.*,c.id as thisid, t.name FROM ".$tbl_prefix."content c 
	LEFT JOIN ".$tbl_prefix."topics t on c.topic=t.topicid 
	WHERE c.public='1'"; 
	if($topic != '0') { $sql .= " AND c.topic='".intval($topic)."'"; }
	$sql .= " ORDER BY c.pubdate DESC LIMIT 0,".intval($length); 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	}  
	while($row = mysql_fetch_assoc($result)) { 
		$row['tags'] = get_tags($row['thisid']);
		$x[] = $row;
	} 	
	return $x;
}

function get_rss_comments($length=10) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT cm.*,cn.pubdate,cn.title as storytitle,cn.id as cid FROM ".$tbl_prefix."comments cm
	LEFT JOIN ".$tbl_prefix."content cn on cm.id=cn.id
	WHERE cn.public='1' AND cm.score>0 ORDER BY cm.commentid DESC LIMIT 0,".intval($length); 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	while($row = mysql_fetch_assoc($result)) { 
		$x[] = $row;
	} 	
	return $x;
}

function urlify($s) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$slug = str_replace(" ","-",strip_tags(trim(stripslashes($s))));
	$slug = str_replace("/","",$slug);
	$slug = str_replace("\\","",$slug);
	$slug = str_replace("#","",$slug);
	$slug = str_replace("?","",$slug);
	$slug = str_replace("&","-",$slug);	
	$slug = str_replace(",","-",$slug);
	$slug = str_replace("--","-",$slug); 
	$slug = str_replace('"',"",$slug); 
	$slug = str_replace("'","",$slug); 	
	$slug = str_replace("--","-",$slug);
	return $slug;
}

function permalink_title_from_db($id) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	$sql = "SELECT title,slug FROM ".$tbl_prefix."content WHERE id='$id'"; 
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { die("Error querying the MySQL database"); } 
	$x = mysql_fetch_assoc($result);
	if($x['slug']) { 
		return $x['slug']; 
	} else { 
		$wtitle = str_replace(" ","_",strip_tags(strtolower(trim(stripslashes($x['title']))))); 
		$wtitle = str_replace("/","",$wtitle);
		$wtitle = str_replace("\\","",$wtitle);
		$wtitle = str_replace("#","",$wtitle);
		$wtitle = str_replace("?","",$wtitle);
		$wtitle = str_replace("&","_",$wtitle);	
		$wtitle = str_replace(",","",$wtitle);
		$wtitle = str_replace("__","_",$wtitle); 
		$wtitle = str_replace('"',"",$wtitle); 
		$wtitle = str_replace("'","",$wtitle); 
		$ptitle = $wtitle; 
		return $ptitle;
	}
}


function search_all_posts($term) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	global $errors, $debug_mode; 
	$sql  = "SELECT * FROM ".$tbl_prefix."content WHERE public='1' AND MATCH (title,teaser,post) AGAINST ('"
	. $term."' IN BOOLEAN MODE) ORDER BY pubdate DESC LIMIT 0,50"; 	
	$result = mysql_query($sql); 
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	while ($row = mysql_fetch_assoc($result)) { 
		$matches[] = $row;
	} 
	return $matches; 
} 


function get_all_users($sort) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	global $errors, $debug_mode; 
	$sql  = "SELECT * FROM ".$tbl_prefix."users";
	$result = mysql_query($sql); 
	if (!$result) { 
		die($errors['mysql']); 
	} else { 
		while($row = mysql_fetch_assoc($result)) { 
			$matches[$row[$sort]] = $row; 
		}
		mysql_free_result($result); 
	}
	return $matches;  	
}

function get_stats($stat) { 
	$tbl_prefix = $GLOBALS['tbl_prefix']; 
	global $errors, $debug_mode; 
	if($stat=="mostcomments") { 
		$sql = "SELECT cn.id,cn.title, COUNT(cm.commentid) AS mycount 
		FROM ".$tbl_prefix."comments cm, ".$tbl_prefix."content cn
		WHERE cn.id=cm.id 
		GROUP BY cn.id 
		ORDER BY mycount DESC LIMIT 0,10";
	} elseif($stat=="poptags") { 
		$sql = "SELECT t.tag,t.tagname,COUNT(t.tagname) AS mycount 
		FROM ".$tbl_prefix."tags t
		GROUP BY t.tagname 
		ORDER BY mycount DESC LIMIT 0,10";
	} elseif($stat=="mostviewed") { 
		$sql = "SELECT ".$tbl_prefix."content.id, ".$tbl_prefix."content.title, ".$tbl_prefix."content.views 
		FROM ".$tbl_prefix."content 
		GROUP BY ".$tbl_prefix."content.id ORDER BY views DESC LIMIT 0,10";
	} elseif($stat=="newestcomments") { 
		$sql = "SELECT c.commentid,n.id,c.title,n.title as ntitle 
		FROM ".$tbl_prefix."comments c 
		LEFT JOIN ".$tbl_prefix."content n ON c.id=n.id 
		ORDER BY c.commentid DESC LIMIT 0,10"; 		
	}
	$result = mysql_query($sql); $GLOBALS['q']++;
	if(!$result) { 
		if($debug_mode) { die(mysql_error()."<br /><br />Query: ".$sql); } else { die($errors['mysql']); } 
	} 
	$n = mysql_num_rows($result); 
	if($n < 1) { 
		return FALSE; 
	} else { 
		while($row = mysql_fetch_assoc($result)) { 
			$x[] = $row;
		} 		
		return $x;
	}	
}

?>